package com.aida.evil.eda.Network;

import android.os.Handler;
import android.support.v4.app.Fragment;

import com.aida.evil.eda.Data.NetworkData;
import com.aida.evil.eda.Data.UserData;
import com.aida.evil.eda.Toast.CustomSuperActivityToast;
import com.aida.evil.eda.Volley.VolleyHandler;
import com.aida.evil.eda.Volley.VolleyHttpRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yalantis.phoenix.PullToRefreshView;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/****************************************************
 * com.aida.evil.eda.Network.Eda_item_network
 * 作者：白雨濃  evil
 * 时间：16-2-18 19:25
 * 邮箱：evilbai@foxmail.com
 * 页面用途：首页面的网络加载，继承Fragment是位置传递给Eda.class
 ****************************************************/
public abstract class Eda_item_network extends Fragment
{
    protected PullToRefreshView pullToRefreshView;//下拉刷新菜单,因为这两个类都要用，所以设置成protected

    /*加载item文字内容*/
    protected final static String URL =
            "http://" + NetworkData.ServiceIP + "/EdaItem?"
                    + "IMEI=" + UserData.DATA_IMEI + "&LastItem=";
    /*加载item图片内容*/
    protected final static String IMGURL =
            "http://" + NetworkData.ServiceIP + "/EdaItem_Img?ItemID=";

    private String IMEI;
    protected int ItemNUM;
    private List<Item内容> ItemList = new ArrayList<>();

    protected abstract void Responselistener(String IMEI, int ItemNUM, List<Item内容> ItemList);

    protected void toNetwork(int LastItemID, final Handler refreshHandler)
    {
        VolleyHandler<String> stringVolleyHandler = new VolleyHandler<String>()
        {
            @Override
            public void reqSuccess(String response)
            {
                Item item = new Item();
                /*解析JSON*/
                {
                    Type type = new TypeToken<Item>()
                    {
                    }.getType();

                    item = new Gson().fromJson(response, type);
                }
                /*操作*/
                if (item != null)
                {
                    IMEI = item.get请求手机的IMEI();
                    ItemNUM = item.get条数();
                    if (IMEI.equals(UserData.DATA_IMEI) && ItemNUM != 0)
                    {
                        ItemList = item.getList();
                        Responselistener(IMEI, ItemNUM, ItemList);
                    }
                    else
                    {
                        CustomSuperActivityToast.makeText(getActivity(), "啊欧，暂时没有东西了，稍后再试试吧！", "好吧……").show();
                    }
                }
                //设置下拉刷新是否重复刷新，如果加载成功，就不需要刷新了，设置成false，如果超时加载，就需要重复刷新
                //pullToRefreshView.setRefreshing(false);
                refreshHandler.sendEmptyMessage(0);
            }

            @Override
            public void reqError(String error)
            {
                CustomSuperActivityToast.makeText(getActivity(), "刷新失败", "刷新").show();
                //pullToRefreshView.setRefreshing(false);
                refreshHandler.sendEmptyMessage(0);
            }
        };

        VolleyHttpRequest.String_request(URL + LastItemID,stringVolleyHandler);
    }


    protected class Item
    {
        private String 请求手机的IMEI;
        private int 条数;
        private List<Item内容> list = new ArrayList();


        public String get请求手机的IMEI()
        {
            return 请求手机的IMEI;
        }

        public void set请求手机的IMEI(String 请求手机的imei)
        {
            请求手机的IMEI = 请求手机的imei;
        }

        public int get条数()
        {
            return 条数;
        }

        public void set条数(int 条数)
        {
            this.条数 = 条数;
        }

        public List<Item内容> getList()
        {
            return list;
        }

        public void setList(List<Item内容> list)
        {
            this.list = list;
        }

    }

    protected class Item内容
    {
        private int ItemID;
        private String ItemImg;
        private String ItemTitle;
        private String ItemContext;

        public int getItemID()
        {
            return ItemID;
        }

        public void setItemID(int itemID)
        {
            ItemID = itemID;
        }

        public String getItemImg()
        {
            return ItemImg;
        }

        public void setItemImg(String itemImg)
        {
            ItemImg = itemImg;
        }

        public String getItemTitle()
        {
            return ItemTitle;
        }

        public void setItemTitle(String itemTitle)
        {
            ItemTitle = itemTitle;
        }

        public String getItemContext()
        {
            return ItemContext;
        }

        public void setItemContext(String itemContext)
        {
            ItemContext = itemContext;
        }

    }
}

